{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (3633590423.py, line 93)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[2], line 93\u001b[1;36m\u001b[0m\n\u001b[1;33m    gobal image\u001b[0m\n\u001b[1;37m          ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import tkinter\n",
    "import tkinter.simpledialog\n",
    "import tkinter.colorchooser\n",
    "import tkinter.filedialog\n",
    "#需要执行pip install pillow安装扩展库\n",
    "from PIL import ImageGrab\n",
    "\n",
    "root = tkinter.Tk()\n",
    "root.title('My Paint---by Dong Fuguo')\n",
    "root['width']=800\n",
    "root['height']=600\n",
    "\n",
    "#控制是否允许画图的变量，1：允许，0：不允许\n",
    "canDraw = tkinter.IntVar(value=0) \n",
    "\n",
    "#控制画图类型的变量，1：曲线，2：直线，3：矩形，4：文本，5：橡皮\n",
    "what=tkinter.IntVar(value=1)\n",
    "\n",
    "#记录鼠标位置的变量\n",
    "X = tkinter.IntVar(value=0)\n",
    "Y = tkinter.IntVar(value=0)\n",
    "\n",
    "#前景色，背景色\n",
    "foreColor = '#000000'\n",
    "backColor = '#FFFFFF'\n",
    "\n",
    "#创建画布\n",
    "image=tkinter.photolmage()\n",
    "canvas=tkinter.Canvas(root,bg='white',width=800,height=600)\n",
    "canvas.create_image(800,600,image=image)\n",
    "\n",
    "#鼠标左键单击，允许画图，并记录当前鼠标位置\n",
    "def onLeftButtonDown(event):\n",
    "    canDraw.set(1)\n",
    "    X.set(event.x)\n",
    "    Y.set(event.y)\n",
    "    if what.get()==4:\n",
    "       canvas.create_text(event.x,event.y,text=text)\n",
    "canvas.bind('<Button-1>',onLeftButtonDown)\n",
    "\n",
    "#记录最后绘制图形的ID\n",
    "lastDraw = 0\n",
    "#按住鼠标左键移动，画图\n",
    "def onLeftButtonMove(event):\n",
    "   global lastDraw\n",
    "   if canDraw.get()==0:\n",
    "       return\n",
    "   if what.get()==1:\n",
    "      #使当前选择的前景色绘制曲线\n",
    "      canvas.create_line(X.get(),Y.get(),event.x,event.y,fill=foreColor)\n",
    "      X.set(event.x)\n",
    "      Y.set(event.y)\n",
    "   elif what.get()==2:\n",
    "      #绘制直线，先删除刚刚画过的直线，再画一条新的直线\n",
    "      try:\n",
    "         canvas.delete(lastDraw)\n",
    "      except Exception as e:\n",
    "         pass\n",
    "      lastDraw = canvas.create_line(X.get(),Y.get(),event.x,event.y,fill=foreColor)\n",
    "   elif what.get()==3:\n",
    "      #绘制矩形，先删除刚刚画过的矩形，再画一个新矩形\n",
    "      try:\n",
    "         canvas.delete(lastDraw)\n",
    "      except Exception as e:\n",
    "         pass\n",
    "      lastDraw = canvas.create_rectangle(X.get(),Y.get(),event.x,event.y,fill=foreColor,outline=foreColor)\n",
    "   elif what.get()==5:\n",
    "      #橡皮，使用背景色填充10*10的矩形区域\n",
    "      canvas.create_rectangle(event.x-5,event.y-5,event.x+5,event.y+5,outline=backColor,fill=backColor)\n",
    "canvas.bind('<B1-Motion>',onLeftButtonMove)\n",
    "\n",
    "#鼠标左键抬起，不允许再画图了\n",
    "def onLeftButtonUp(event):\n",
    "  if what.get()==2:\n",
    "     #绘制直线\n",
    "     canvas.create_line(X.get(),Y.get(),event.x,event.y,fill=foreColor)\n",
    "  elif what.get()==3:\n",
    "     #绘制矩形\n",
    "     canvas.create_rectangle(X.get(),Y.get(),event.x,event.y,fill=foreColor,outline=foreColor)\n",
    "  canDraw.set(0)\n",
    "  global lastDraw\n",
    "  #防止切换图形时误删上次绘制的图形\n",
    "  lastDraw = 0\n",
    "canvas.bind('<ButtonRelease-1>',onLeftButtonUp)\n",
    "\n",
    "#创建菜单\n",
    "menu = tkinter.Menu(root, tearoff=0)\n",
    "#打开图像文件\n",
    "def Open():\n",
    "  filename = tkinter.filedialog.askopenfilename(title='Open Image',filetypes=[('image','*.jpg*.png*.gif')])\n",
    "  if filename:\n",
    "      gobal image \n",
    "      image =tkinter.PhotoImage(file=filename)\n",
    "      canvas.create_image(80,80,image=image)\n",
    "menu.add_command(label='Open',command=Open)\n",
    "\n",
    "def Save():\n",
    "  #获取客户区域位置和尺寸，并截图保存\n",
    "  left=int(root.winfo_x())\n",
    "  top=int(root.winfo_y())\n",
    "  width=root.winfo_width()\n",
    "  height=root.winfo_height()\n",
    "  im=ImageGrab.grab((left,top,left+width,top+height))\n",
    "\n",
    "  #保存绘制的图片\n",
    "  filename=tkinter.filedialog.asksaveasfilename(title='保存图片',filetypes=[('图片文件','*.png')])\n",
    "  if not filename:\n",
    "    return\n",
    "  if not filename.endswith('.png'):\n",
    "     filename=filename+'.png'\n",
    "  im.save(filename)\n",
    "menu.add_command(label='Save',command=Save)\n",
    "\n",
    "#添加菜单，清除，遍历画布上的所有图形，逐个删除\n",
    "def Clear():\n",
    "  for item in canvas.find_all():\n",
    "    canvas.delete(item)\n",
    "menu.add_command(label='Clear',command=Clear)\n",
    "\n",
    "#添加分割线\n",
    "menu.add_separator()\n",
    "\n",
    "#创建子菜单，用来选择绘图类型\n",
    "menuType=tkinter.Menu(menu,tearoff=0)\n",
    "def drawCurve():\n",
    "  what.set(1)\n",
    "menuType.add_command(label='Curve',command=drawCurve)\n",
    "\n",
    "def drawLine():\n",
    "  what.set(2)\n",
    "menuType.add_command(label='Line',command=drawLine)\n",
    "\n",
    "def drawRectangle():\n",
    "  what.set(3)\n",
    "menuType.add_command(label='Rectangle',command=drawRectangle)\n",
    "\n",
    "def drawText():\n",
    "  global text\n",
    "  text=tkinter.simpledialog.askstring(title='Input what you want to draw',prompt='')\n",
    "  what.set(4)\n",
    "menuType.add_command(label='Text',command=drawText)\n",
    "menuType.add_separator()\n",
    "\n",
    "#选择前景颜色\n",
    "def chooseForeColor():\n",
    "  global foreColor\n",
    "  foreColor=tkinter.colorchooser.askcolor()[1]\n",
    "menuType.add_command(label='Choose Foreground Color',command=chooseForeColor)\n",
    "\n",
    "#橡皮\n",
    "def onErase():\n",
    "  what.set(5)\n",
    "menuType.add_command(label='Erase',command=onErase)\n",
    "\n",
    "menu.add_cascade(label='Type',menu=menuType)\n",
    "\n",
    "#鼠标右键抬起，弹出菜单\n",
    "def onRightButtonUp(event):\n",
    "  menu.post(event.x_root,event.y_root)\n",
    "canvas.bind('<ButtonRelease-3>',onRightButtonUp)\n",
    "\n",
    "cavas.pack(fill=tkinter.BOTH,expand=tkinter.YES)\n",
    "\n",
    "#启动应用程序\n",
    "root.mainloop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
